<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$_SESSION[TINYMCE_UPLOAD] = 'uploads/tinymce/';

$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', NULL);
$item_id = MiscUtils::getParam('id', 0);

$item = NULL;
$cond_vals = new stdClass();
$cond_vals->c = 't.id = :v1';
$cond_vals->v = array(':v1' => $item_id);
$tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_mthd_delivery', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
if (count($tmp) > 0) {
  $item = $tmp[0];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #222;
  border-left: 1px solid #222;
}
table.form th.header {
  text-align: left;
  color: #fff;
  background-color: #222;
  padding-left: 5px;
  font-weight: bold;
  height: 25px;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
}

table.form td {
  vertical-align: middle;
}
table.form td.title {
  background-color: #666;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
  width: 150px;
  color: #fff;
  padding: 3px 5px; 
  vertical-align: top;
  font-weight: bold;
}
table.form td.content {
  padding-left: 2px;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
  width: 610px;
}
table.form td.content input[type=text],textarea,select,option {
  width: 98%;
  border: 0;
  padding: 0;
  margin: 0;
}
table.form td.content select {
  border: 1px solid #ddd;
}
table.form td.content input[type=text] {
  height: 20px;
}
table.form td.content textarea {
  height: 50px;
  resize: none;
}
.imgbtn {
  background-image: url(assets/imgs/save.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}
ul.hztl li {
  display: inline;
  vertical-align: top;
}
table.form4 {
  background-color: #e9e9e9;
  border: 1px solid #666;
  color: #000;
  padding: 2px;
  padding-bottom: 20px;
  cursor: pointer;
  display: inline-block;
  margin: 2px;
  font-weight: bold;
}
</style>
</head>
<body>
<div>
  <table class="form" cellpadding=0 cellspacing=0>
    <tr><th class="header" colspan=2>Edit Delivery</th></tr>
    <tr>
      <td class="title">Name</td>
      <td class="content"><input value="<?php echo (($item) ? $item->title : ''); ?>" id="title" type="text"></input></td>
    </tr>
    <tr>
      <td class="title">Price</td>
      <td class="content"><input value="<?php echo (($item) ? $item->price : ''); ?>" id="price" type="text"></input></td>
    </tr>
    <tr>
      <td class="title">Available</td>
      <td class="content"><input <?php if (!$item || ($item && $item->status == 1)) { echo "checked=checked"; }; ?>" id="status" type="checkbox"></input></td>
    </tr>
    <tr>
	    <td class="title">Description</td>
	    <td class="content"><textarea id="description" ><?php echo ($item ? $item->description : ''); ?></textarea></td>
	  </tr>
  </table>
  <button class="imgbtn" onclick="save();" style="margin-top: 5px;">Save</button>
  <div style="height: 200px"></div>
</div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
  $(function(){
    //NOT GOOD
    var div = parent.document.getElementById('<?php echo LAST_NAV; ?>');
    var div_parent = div.parentNode;
    var value = div_parent.firstChild.nodeValue;
    MyUtils.removeChildElements(div_parent);
    var elm;
    elm = parent.document.createElement('a');
    elm.href = '#';
    elm.style.color = '#000';
    elm.style.fontSize = '13px';
    elm.style.fontWeight = 'bold';
    elm.onclick = function() {
      parent.location.href = 'http://<?php echo URL . 'admin.php?i=' . $page_id; ?>';
    };
    elm.appendChild(parent.document.createTextNode(value));
    div_parent.appendChild(elm);

    var td;
    td = div_parent.parentNode.insertCell(-1);
    td.style.width = '20px';
    td.style.textAlign = 'center';
    td.style.color = '#aaa';
    td.appendChild(parent.document.createTextNode('>'));
    
    td = div_parent.parentNode.insertCell(-1);
    td.style.height = '25px';
    td.style.color = '#333';
    td.style.fontSize = '13px';
    <?php if ($item) { ?> 
    td.title = '<?php echo $item->title; ?>';
    td.appendChild(parent.document.createTextNode(MyUtils.getShort('<?php echo $item->title; ?>', 10)));
    <?php } else { ?>
    td.appendChild(parent.document.createTextNode('New Delivery'));
    <?php } ?>
    //NOT GOOD END

    tinyMCE.init( {
      // General options
      mode: 'exact',
      elements: 'description',
      theme: 'advanced',

      plugins: 'pdw,filebrowser,autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist',
      // Theme options
      theme_advanced_buttons1: 'pdw_toggle,|,undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,|,forecolor,backcolor,|,image,media,|,preview',
      theme_advanced_buttons2: 'search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime',
      theme_advanced_buttons3: 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,advhr,|,print,|,ltr,rtl,|,fullscreen',
      theme_advanced_buttons4: 'insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak',
      theme_advanced_toolbar_location: 'top',
      theme_advanced_toolbar_align: 'left',
      theme_advanced_statusbar_location: 'bottom',
      theme_advanced_resizing: true,

      file_browser_callback: 'filebrowser',
      pdw_toggle_on: '1',
      pdw_toggle_toolbars: '2,3,4',

      // Example content CSS (should be your site CSS)
      theme_advanced_font_sizes: '10px,11px,12px,13px,14px,15px,16px,17px,18px,19px,20px,21px,22px,23px,24px,25px,26px,27px,28px,29px,30px',
    });
  });

  function save() {
    var obj = {
      <?php if ($item) { ?>
      id: '<?php echo $item->id; ?>',
      <?php } ?>
      title: document.getElementById('title').value,
      price: document.getElementById('price').value,
      status: (document.getElementById('status').checked) ? 1 : 0,
      description: tinyMCE.get('description').getContent()
    }
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=save&q=" + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        var obj = JSON.parse(msg);
        parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&script=index_delivery&id="; ?>' + obj.id;
      }
    });
  }
</script>
</body>
</html>